package com.yeyks.controller.finance;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.NeedToken;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.*;
import com.yeyks.common.result.*;
import com.yeyks.common.utils.excel.ExcelUtil;
import com.yeyks.commonReference.service.order.OrderHeadService;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 酒店对账单接口
 *
 * @author shuai
 * @date 2019-07-09
 */
@Slf4j
@Api(tags = "酒店对账单接口")
@RestController
@RequestMapping("/hotel/statement")
public class HotelStatementController {
    @Autowired
    private OrderHeadService orderHeadService;

    /**
     * C2B财务对账单统计
     *
     * @return
     */
    @NeedToken
    @ApiOperation(value = "酒店C2B财务对账单统计", notes = "酒店C2B财务对账单统计", response = StatisticalHotelResults.class)
    @PostMapping("/stat")
    public AjaxResponseBody<List<StatisticalHotelResults>> stat(@Validated HotelStatementStatParam param) {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (AccountType.HotelManager != loginUser.getType()) {
            throw new ServiceException("你无权操作");
        }
        List<StatisticalHotelResults> hotelStatementResults = orderHeadService.hotelStatementStat(loginUser, param);
        return AjaxResponseBody.success(hotelStatementResults);
    }

    /**
     * C2B财务对账单统计下载
     *
     * @return
     */
    @NeedToken
    @ApiOperation(value = "酒店C2B财务对账单统计下载", notes = "酒店C2B财务对账单统计下载", response = StatisticalHotelDownLoadResult.class)
    @PostMapping("/stat/download")
    public void statDownload(HotelStatementStatDownLoadParam param, HttpServletResponse response) {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (AccountType.HotelManager != loginUser.getType()) {
            throw new ServiceException("你无权操作");
        }
        List<StatisticalHotelDownLoadResult> hotelStatementResults = orderHeadService.hotelStatementStatDownload(loginUser, param);
        ExcelUtil.disposeWriteExcelWithSheets(response, hotelStatementResults, new StatisticalHotelDownLoadResult());
    }

}
